﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for Class1
/// </summary>
public class ConnectionData
{
    String conn;
    SqlConnection sqlconn;
    public ConnectionData()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    //phuong thuc mo ket noi
    public void Connecting()
    {
        conn = ConfigurationSettings.AppSettings["conn"];
        sqlconn = new SqlConnection(conn);
        sqlconn.Open();
    }
    // Phuong thuc dong ket noi
    public void Disconnect()
    {
        sqlconn.Close();
    }
    //phuong thuc thuc thi cau truy van
    public DataSet ExecuteQuery(String sql)
    {
        Connecting();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sql,conn);
        da.Fill(ds);
        Disconnect();
        return ds;
    }

    public int LayId(String sql)
    {
        int i = 0;
        Connecting();
        SqlCommand cmd = new SqlCommand(sql, sqlconn);
        SqlDataReader rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            if (rd["dia_oc_id"] == null)
                i = 0;
            else
                i = Convert.ToInt32(rd["dia_oc_id"]);
            break;
        }

        Disconnect();
        return i;
    }
    //phuong thuc lay so lan xem tin
    public int LayLuotXem(String sql)
    {
        int i = 0;
        Connecting();
        SqlCommand cmd = new SqlCommand(sql, sqlconn);
        SqlDataReader rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            if (rd["So_Lan_Xem"] == null)
                i = 0;
            else
                i = Convert.ToInt32(rd["So_Lan_Xem"]);
            break;
        }

        Disconnect();
        return i;
    }

    //lay du lieu 2 dong cuoi
    public SqlCommand ExecuteQueryReader(String sql)
    {
        
        Connecting();
        SqlCommand cmd = new SqlCommand(sql,sqlconn);      
        return cmd;
    }

    public int ExecuteSQL(String sql)
    {
        try
        {
            Connecting();
            SqlCommand cmd = new SqlCommand(sql, sqlconn);
            cmd.ExecuteNonQuery();
            Disconnect();
            return 1;
        }
        catch(Exception e)
        {
            Disconnect();
            return 0;
        }
    }
    public int LayDangNhap(String sql)
    {
        int i = 0;
        Connecting();
        SqlCommand cmd = new SqlCommand(sql, sqlconn);
        SqlDataReader rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            i = 1;
            break;
        }

        Disconnect();
        return i;
    }
    public void dangnhap(string sql)
    {
        Connecting();

        SqlCommand sqlcom = new SqlCommand(sql, sqlconn);
        sqlcom.ExecuteNonQuery();
        Disconnect();
    }

    public int ktTonTai(String sql)
    {
        int i = 0;
        try
        {
            Connecting();
            SqlCommand cmd = new SqlCommand(sql, sqlconn);
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                return Convert.ToInt32(rd[0]);
            }
            Disconnect();
        }
        catch (Exception e)
        {
            Disconnect();
            return 0;
        }
        return i;
    }


}